package com.sgb.kjwl.view.widget.packers;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.PickSelectEntity2;
import com.sgb.kjwl.view.ui.adapter.YoungBaseRecycleAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : younghow
 * @date : 2021/9/23 10:02
 * description:
 */
public abstract class ThreeLevelPopWindow <D extends PickSelectEntity2,A extends YoungBaseRecycleAdapter<D>,B extends YoungBaseRecycleAdapter<D>,C extends YoungBaseRecycleAdapter<D>> extends BasePopWindow {

    private A mProvinceAdapter;
    private B mCityAdapter;
    private C mAreaAdapter;
    private RecyclerView rvProvince;
    private RecyclerView rvCity;
    private RecyclerView rvArea;
    private TextView okView;
    private TextView cancelView;

    public ThreeLevelPopWindow(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.three_level_for_select_area;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        okView = holder.getPop().findViewById(R.id.ok_view);
        cancelView = holder.getPop().findViewById(R.id.cancel_view);

        rvProvince = holder.getPop().findViewById(R.id.rv_province);
        rvCity = holder.getPop().findViewById(R.id.rv_city);
        rvArea = holder.getPop().findViewById(R.id.rv_area);

        mProvinceAdapter = createProvinceAdapter();
        mCityAdapter = createCityAdapter();
        mAreaAdapter = createAreaAdapter();

        rvProvince.setLayoutManager(createProvinceLayoutManage());
        rvCity.setLayoutManager(createCityLayoutManage());
        rvArea.setLayoutManager(createAreaLayoutManage());

        rvProvince.setAdapter(mProvinceAdapter);
        rvCity.setAdapter(mCityAdapter);
        rvArea.setAdapter(mAreaAdapter);

        okView.setOnClickListener(v -> {
            createOkMethod();
            dissMiss();
        });

        cancelView.setOnClickListener(v -> createCancelMethod());
    }

    public void setProvinceDefaultItem(int position){
        mProvinceAdapter.setDefItem(position);
        mProvinceAdapter.setCheckDefault(position);
    }

    public int setProvinceDefaultItemForNo(String position){
        int p = -1;
        for (int i = 0; i < mProvinceAdapter.getDatas().size(); i++) {
            if (position.equals(mProvinceAdapter.getDatas().get(i).getNo())){
                mProvinceAdapter.setDefItem(i);
                mProvinceAdapter.setCheckDefault(i);
                p = i;
            }
        }
        return p;
    }

    public void setProvinceDefaultItems(int position){
        mProvinceAdapter.setDefItem(position);
        mProvinceAdapter.setCheckDefault(position);
    }

    public void setCityDefaultItem(int position){
        mCityAdapter.setDefItem(position);
        mCityAdapter.setCheckDefault(position);
    }

    public void setCityDefaultItemForNo(String position){
        for (int i = 0; i < mCityAdapter.getDatas().size(); i++) {
            if (position.equals(mCityAdapter.getDatas().get(i).getName())){
                mCityAdapter.setDefItem(i);
                mCityAdapter.setCheckDefault(i);
            }
        }
    }

    public void setCityDefaultItems(int...position) {
        for (int i : position) {
            mCityAdapter.setDefItem(i);
            mCityAdapter.setCheckDefault(i);
        }
    }

    public void setAreaDefaultItem(int position){
        mAreaAdapter.setDefItem(position);
        mAreaAdapter.setCheckDefault(position);
    }

    public void setAreaDefaultItems(int position){
        mAreaAdapter.setDefItem(position);
        mAreaAdapter.setCheckDefault(position);
    }

    public void setAreaListVisible(boolean visible){
        if (visible){
            rvArea.setVisibility(View.VISIBLE);
        }else {
            rvArea.setVisibility(View.GONE);
        }
    }

    protected abstract A createProvinceAdapter();

    protected abstract B createCityAdapter();

    protected abstract C createAreaAdapter();

    protected abstract RecyclerView.LayoutManager createProvinceLayoutManage();

    protected abstract RecyclerView.LayoutManager createCityLayoutManage();

    protected abstract RecyclerView.LayoutManager createAreaLayoutManage();

    protected abstract void createOkMethod();

    protected abstract void createCancelMethod();

    protected TextView providerOkView(){
        return okView;
    }

    protected TextView providerCancelView(){
        return cancelView;
    }

    protected void setAreaViewVisible(boolean flag){
        rvArea.setVisibility(flag ? View.VISIBLE : View.GONE);
    }

    protected RecyclerView providerProvinceView(){
        return rvProvince;
    }

    protected RecyclerView providerCityView(){
        return rvCity;
    }

    public interface SelectResultClick<D> {

        void result(List<D> pickSelectEntitiesProvince,List<D> pickSelectEntitiesCity,List<D> pickSelectEntitiesArea);
        void reset();
    }
}
